﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows.Forms;
using PolePosition.v3.EntityLibrary;

namespace PolePosition.v3.Desktop.Grids
{
    public partial class GridDistributionInspection : GridBase, IGridBase
    {
        public GridDistributionInspection()
            : base(Common.Enumerations.DataTypes.ProjectsDistributionInspection)
        {
            InitializeComponent();
            advTreeInspectionVisits.Visible = false;
            this.contextMenuBarGrid.SetContextMenuEx(this.advTreeInspectionVisits, this.buttonItemContextMenu);
        }

        private bool m_CombosBound = false;
        private void BindCombos()
        {
            if (Program.LookupTableData != null && !m_CombosBound)
            {
                bindingSourceMap.DataSource = Program.LoginResult.Polygons;
                comboBoxExMap.DisplayMember = EntityLibrary.UtilityPolygonEnumerations.Columns.Name.ToString();
                comboBoxExMap.ValueMember = EntityLibrary.UtilityPolygonEnumerations.Columns.Utility_PolygonId.ToString();
                comboBoxExMap.DataSource = bindingSourceMap;

                bindingSourcePoleSpecies.DataSource = Program.LookupTableData.XMissionStructuresPoleSpecies;
                comboBoxExPoleSpecies.DisplayMember = EntityLibrary.XMissionStructuresPoleSpeciesEnumerations.Columns.Name.ToString();
                comboBoxExPoleSpecies.ValueMember = EntityLibrary.XMissionStructuresPoleSpeciesEnumerations.Columns.XMissionStructures_PoleSpeciesId.ToString();
                comboBoxExPoleSpecies.DataSource = bindingSourcePoleSpecies;

                bindingSourcePoleClass.DataSource = Program.LookupTableData.DistributionClasses;
                comboBoxExPoleClass.DisplayMember = EntityLibrary.DistributionClassesEnumerations.Columns.Name.ToString();
                comboBoxExPoleClass.ValueMember = EntityLibrary.DistributionClassesEnumerations.Columns.ClassId.ToString();
                comboBoxExPoleClass.DataSource = bindingSourcePoleClass;

                bindingSourcePoleTreatment.DataSource = Program.LookupTableData.XMissionStructuresPoleTreatments;
                comboBoxExPoleTreatment.DisplayMember = EntityLibrary.XMissionStructuresPoleTreatmentEnumerations.Columns.Name.ToString();
                comboBoxExPoleTreatment.ValueMember = EntityLibrary.XMissionStructuresPoleTreatmentEnumerations.Columns.XMissionStructures_PoleTreatmentId.ToString();
                comboBoxExPoleTreatment.DataSource = bindingSourcePoleTreatment;

                bindingSourcePoleLength.DataSource = Program.LookupTableData.DistributionLengths;
                comboBoxExPoleLength.DisplayMember = EntityLibrary.DistributionLengthEnumerations.Columns.Name.ToString();
                comboBoxExPoleLength.ValueMember = EntityLibrary.DistributionLengthEnumerations.Columns.LengthId.ToString();
                comboBoxExPoleLength.DataSource = bindingSourcePoleLength;

                bindingSourceResultType.DataSource = Program.LookupTableData.VendorInspVisitXmissionStructuresResultTypes;
                comboBoxExResultType.DisplayMember = EntityLibrary.VendorInspVisitXmissionStructuresResultTypeEnumerations.Columns.Name.ToString();
                comboBoxExResultType.ValueMember = EntityLibrary.VendorInspVisitXmissionStructuresResultTypeEnumerations.Columns.Vendor_InspVisit_XmissionStructures_ResultTypeId.ToString();
                comboBoxExResultType.DataSource = bindingSourceResultType;

                bindingSourceInspectionType.DataSource = Program.LookupTableData.VendorInspVisitXmissionStructuresInspectionType;
                comboBoxExInspectionType.DisplayMember = EntityLibrary.VendorInspVisitXmissionStructuresInspectionTypeEnumerations.Columns.Name.ToString();
                comboBoxExInspectionType.ValueMember = EntityLibrary.VendorInspVisitXmissionStructuresInspectionTypeEnumerations.Columns.Vendor_InspVisit_XmissionStructures_Inspection_TypeId.ToString();
                comboBoxExInspectionType.DataSource = bindingSourceInspectionType;

                bindingSourceNoteType.DataSource = Program.LookupTableData.DistributionMeterNotes;
                comboBoxExMeterNoteType.DisplayMember = EntityLibrary.DistributionMeterNoteEnumerations.Columns.Name.ToString();
                comboBoxExMeterNoteType.ValueMember = EntityLibrary.DistributionMeterNoteEnumerations.Columns.MeterNoteId.ToString();
                comboBoxExMeterNoteType.DataSource = bindingSourceNoteType;

                bindingSourceElectricalProblem.DataSource = ComboOptions.YesNoForBoolean;
                comboBoxExElectricalProblem.DisplayMember = "Text";
                comboBoxExElectricalProblem.ValueMember = "Value";
                comboBoxExElectricalProblem.DataSource = bindingSourceElectricalProblem;

                bindingSourceGroundWireRepair.DataSource = ComboOptions.YesNoForBoolean;
                comboBoxExGroundWireRepair.DisplayMember = "Text";
                comboBoxExGroundWireRepair.ValueMember = "Value";
                comboBoxExGroundWireRepair.DataSource = bindingSourceGroundWireRepair;

                bindingSourcePoleTag.DataSource = ComboOptions.YesNoForBoolean;
                comboBoxExPoleTag.DisplayMember = "Text";
                comboBoxExPoleTag.ValueMember = "Value";
                comboBoxExPoleTag.DataSource = bindingSourcePoleTag;

                bindingSourceDistributionLookup.DataSource = Program.LookupTableData.VendorDistributionLookups;
                comboBoxExKv.DisplayMember = VendorDistributionLookupsEnumerations.Columns.LookUpText.ToString();
                comboBoxExKv.ValueMember = VendorDistributionLookupsEnumerations.Columns.Vendor_Distribution_LookupId.ToString();
                comboBoxExKv.DataSource = bindingSourceDistributionLookup;

                comboBoxExACSR.DisplayMember = VendorDistributionLookupsEnumerations.Columns.LookUpText.ToString();
                comboBoxExACSR.ValueMember = VendorDistributionLookupsEnumerations.Columns.Vendor_Distribution_LookupId.ToString();
                comboBoxExACSR.DataSource = bindingSourceDistributionLookup;

                comboBoxExConductorType.DisplayMember = VendorDistributionLookupsEnumerations.Columns.LookUpText.ToString();
                comboBoxExConductorType.ValueMember = VendorDistributionLookupsEnumerations.Columns.Vendor_Distribution_LookupId.ToString();
                comboBoxExConductorType.DataSource = bindingSourceDistributionLookup;

                comboBoxExSpec.DisplayMember = VendorDistributionLookupsEnumerations.Columns.LookUpText.ToString();
                comboBoxExSpec.ValueMember = VendorDistributionLookupsEnumerations.Columns.Vendor_Distribution_LookupId.ToString();
                comboBoxExSpec.DataSource = bindingSourceDistributionLookup;

                m_CombosBound = true;
            }
        }

        #region Asynch Processing (Implements IGridBase)

        public override void DoAsynchLoad(DoWorkEventArgs e)
        {
            advTreeInspectionVisits.Invoke(new MethodInvoker(delegate() { advTreeInspectionVisits.Enabled = false; }));

            int _TotalRecords;
            e.Result = PolePosition.v3.ServiceProxy.UtilityService.GetDistributionInspectionProjectData(base.DataKeyId, base.PageSize, base.PageNumber, Common.Enumerations.GridSearchByTpes.None, null, out _TotalRecords);
            base.TotalItems = _TotalRecords;
        }

        public override void AsychLoadComplete(object sender, RunWorkerCompletedEventArgs e)
        {
            base.AsychLoadComplete(sender, e);
            //do whatever needs to be done after asynch work complete (BackgroudWorker_RunWorkerCompleted)
            if (e.Cancelled)
            {

            }

            if (e.Error != null || e.Result == null)
            {
                //todo: create some sort of error dialog and use it here instead of messagebx
                DevComponents.DotNetBar.MessageBoxEx.Show("Error retrieving project data.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }


            List<EntityLibrary.VendorDistributionPoleEntity> _data;
            //_data[0].VendorDistributionTransformerCollection
            _data = e.Result as List<EntityLibrary.VendorDistributionPoleEntity>;
            
            if (_data == null || _data.Count() == 0)
            {
                if (base.CurrentSearchType == Common.Enumerations.GridSearchByTpes.None)
                    DevComponents.DotNetBar.MessageBoxEx.Show("No data found for the selected distribution inspection project.", "No Data Found", MessageBoxButtons.OK, MessageBoxIcon.Information);
                else
                    DevComponents.DotNetBar.MessageBoxEx.Show("No data found for the distribution inspection project with the applied search.", "No Data Found", MessageBoxButtons.OK, MessageBoxIcon.Information);

                return;
            }

            this.bindingSourceGridDataParent.DataSource = _data;
            this.advTreeInspectionVisits.DataSource = bindingSourceGridDataParent;
            this.advTreeInspectionVisits.Bind();
            this.advTreeInspectionVisits.Visible = true;
            advTreeInspectionVisits.Enabled = true;
        }

        public override void DoAsynchSave(DoWorkEventArgs e)
        {
        }

        public override void AsychSaveComplete(object sender, RunWorkerCompletedEventArgs e)
        {

        }
        public override void DoAsynchGetKml(DoWorkEventArgs e)
        {
            advTreeInspectionVisits.Invoke(new MethodInvoker(delegate() { advTreeInspectionVisits.Enabled = false; }));

            base.Kml = PolePosition.v3.ServiceProxy.UtilityService.GetDistributionInspectionProjectKML(
                base.DataKeyId, base.CurrentSearchType, base.CurrentSearchText);
        }

        public override void AsychGetKmlComplete(object sender, RunWorkerCompletedEventArgs e)
        {
            base.AsychGetKmlComplete(sender, e);
            this.advTreeInspectionVisits.Enabled = true;

            if (e.Cancelled)
            {
                // not sure what to do
            }

            if (e.Error != null)
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("Error retrieving project data.",
                    "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            ShowKmlInMap();
        }

        #endregion

        #region Context Menu / Main Bar (Implements IGridBase)

        public override void DoSearch(Common.Enumerations.GridSearchByTpes searchBy, string searchBytext)
        {

        }

        public override void DoExportToExcel()
        {
            //just call method in base
            base.DoExportToExcel();
        }

        public override void DoEdit()
        {
            EntityLibrary.VendorDistributionPoleEntity _Visit;
            _Visit = advTreeInspectionVisits.GetSelectedEntity() as EntityLibrary.VendorDistributionPoleEntity;
            
            Editors.EditDistributionInspectionVisit _Editor = new PolePosition.v3.Desktop.Editors.EditDistributionInspectionVisit(true, _Visit);
            if (_Editor.ShowDialog() == DialogResult.OK)
            {
                //update the record in the grid

            }
            _Editor.Dispose();
        }

        public override void DoOpen()
        {
            EntityLibrary.VendorDistributionPoleEntity _Visit;
            _Visit = advTreeInspectionVisits.GetSelectedEntity() as EntityLibrary.VendorDistributionPoleEntity;

            Editors.EditDistributionInspectionVisit _Editor = new PolePosition.v3.Desktop.Editors.EditDistributionInspectionVisit(false, _Visit);
            _Editor.ShowDialog();
            _Editor.Dispose();
        }

        public override void DoMapForGrid()
        {
            StartAsynchGetKml();
        }

        public override void DoMapForRecord()
        {

        }

        public override bool ContextShowAllowed()
        {
            EntityLibrary.VendorDistributionPoleEntity _Pole;
            _Pole = advTreeInspectionVisits.GetSelectedEntity() as EntityLibrary.VendorDistributionPoleEntity;

            if (_Pole == null)
                return false;

            base.SelectedRowLatitude = _Pole.Latitude;
            base.SelectedRowLongitude = _Pole.Longitude;
    
            return true;
        }
        #endregion

        public void ShowData(int projectId, string projectName)
        {
            base.DataKeyId = projectId;
            BindCombos();
            base.DataDescription = projectName;
            advTreeInspectionVisits.Visible = false;
            StartAsynchLoad();
        }

        private void buttonItemFindByVisit_Click(object sender, EventArgs e)
        {
            base.ShowSearch("Find Visit Number");
        }

        private void buttonItemFindByMap_Click(object sender, EventArgs e)
        {
            base.ShowSearch("Find Map");
        }

        private void advTreeInspectionVisits_Click(object sender, EventArgs e)
        {

        }

    }
}
